package com.exrobot.module.rbt.service.canteenmenu;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.exrobot.module.rbt.controller.admin.canteenmenu.vo.*;
import com.exrobot.module.rbt.dal.dataobject.canteenmenu.CanteenMenuDO;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.util.object.BeanUtils;

import com.exrobot.module.rbt.dal.mysql.canteenmenu.CanteenMenuMapper;

import static com.exrobot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.exrobot.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
import static com.exrobot.framework.common.util.collection.CollectionUtils.convertList;
import static com.exrobot.framework.common.util.collection.CollectionUtils.diffList;
import static com.exrobot.module.rbt.enums.ErrorCodeConstants.*;

/**
 * 食堂菜单 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CanteenMenuServiceImpl implements CanteenMenuService {

    @Resource
    private CanteenMenuMapper canteenMenuMapper;

    @Override
    public Long createCanteenMenu(CanteenMenuSaveReqVO createReqVO) {
        // 插入
        CanteenMenuDO canteenMenu = BeanUtils.toBean(createReqVO, CanteenMenuDO.class);
        canteenMenuMapper.insert(canteenMenu);

        // 返回
        return canteenMenu.getId();
    }

    @Override
    public void updateCanteenMenu(CanteenMenuSaveReqVO updateReqVO) {
        // 校验存在
        validateCanteenMenuExists(updateReqVO.getId());
        // 更新
        CanteenMenuDO updateObj = BeanUtils.toBean(updateReqVO, CanteenMenuDO.class);
        canteenMenuMapper.updateById(updateObj);
    }

    @Override
    public void deleteCanteenMenu(Long id) {
        // 校验存在
        validateCanteenMenuExists(id);
        // 删除
        canteenMenuMapper.deleteById(id);
    }

    @Override
    public void deleteCanteenMenuListByIds(List<Long> ids) {
        // 删除
        canteenMenuMapper.deleteByIds(ids);
    }


    private void validateCanteenMenuExists(Long id) {
        if (canteenMenuMapper.selectById(id) == null) {
            throw exception(RBT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public CanteenMenuDO getCanteenMenu(Long id) {
        return canteenMenuMapper.selectById(id);
    }

    @Override
    public PageResult<CanteenMenuDO> getCanteenMenuPage(CanteenMenuPageReqVO pageReqVO) {
        return canteenMenuMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CanteenMenuDO> getCanteenMenuList(Collection<Long> ids) {
        return canteenMenuMapper.selectByIds(ids);
    }

    @Override
    public List<CanteenMenuDO> getCanteenMenuListByMenuSn(Collection<Long> menuSns) {
        CanteenMenuPageReqVO pageReqVO = new CanteenMenuPageReqVO();
        pageReqVO.setPageSize(PAGE_SIZE_NONE);
        pageReqVO.setMenuSns(menuSns);
        return canteenMenuMapper.selectPage(pageReqVO).getList();
    }
}